Source file ⇒ Galactic_Map.Rmd

This galactic map is my own attempt at trying to replicate Henry M Bernberg’s Star Wars Galaxy Map http://www.swgalaxymap.com/. I do not want to take credit for the data sets used in my replication or the intellectual property of star wars. My goal was simply to test my skills in creating my own version of a Star Wars galaxy map.

{#Assigning json file locations to respective shortened variable name 
PlanetsFile<-"https://raw.githubusercontent.com/kcm5331/Portfolio/master/planets2.geojson"
RegionsFile<-"https://raw.githubusercontent.com/kcm5331/Portfolio/master/region.geojson"
SectorsFile<-"https://raw.githubusercontent.com/kcm5331/Portfolio/master/sector.geojson"
HyperspaceFile<-
  "https://raw.githubusercontent.com/kcm5331/Portfolio/master/hyperspace.geojson"
GridFile<-"https://raw.githubusercontent.com/kcm5331/Portfolio/master/grid.geojson"
}
{#Reading json files into R
Planets<-readOGR(PlanetsFile)
Regions<-readOGR(RegionsFile)
Sectors<-readOGR(SectorsFile)
Hyperspace<-readOGR(HyperspaceFile)
Grid<-readOGR(GridFile)
}
## OGR data source with driver: GeoJSON 
## Source: "https://raw.githubusercontent.com/kcm5331/Portfolio/master/planets2.geojson", layer: "planets2"
## with 2134 features
## It has 17 fields
## OGR data source with driver: GeoJSON 
## Source: "https://raw.githubusercontent.com/kcm5331/Portfolio/master/region.geojson", layer: "region"
## with 11 features
## It has 6 fields
## OGR data source with driver: GeoJSON 
## Source: "https://raw.githubusercontent.com/kcm5331/Portfolio/master/sector.geojson", layer: "sector"
## with 448 features
## It has 7 fields
## OGR data source with driver: GeoJSON 
## Source: "https://raw.githubusercontent.com/kcm5331/Portfolio/master/hyperspace.geojson", layer: "hyperspace"
## with 1561 features
## It has 8 fields
## OGR data source with driver: GeoJSON 
## Source: "https://raw.githubusercontent.com/kcm5331/Portfolio/master/grid.geojson", layer: "grid"
## with 552 features
## It has 4 fields
#Editing dataframes

#List of planets that I personally feel should be more visible from a distance
Alterations<-c(
"Abregado-rae","Aeten","Alderaan",
"Alzoc","Anaxes","Antar",
"Atzerri","Bakura","Bespin",
"Bestine","Bonadan","Bothawui",
"Boz Pity","Carida","Cato Neimoidia",
"Christophsis","Corellia","Corulag",
"Duro","Eriadu","Foerost",
"Fondor","Geonosis","Hypori",
"Ithor","Malastare","Minntooine","Muunilinst",
"Rattatak","Raxus","Rhen Var",
"Rishi","Rodia","Rothana",
"Scipio","Sluis Van","Tibrin",
"Toydaria","Trandosha","Umbara")

PlanetsDF<-as.data.frame(Planets)
  
#Creating a the content for the pop up information of the planets  
PlanetsDF$content<- paste0("Planet: ",PlanetsDF$name,
                             "<br>","Sector: ", PlanetsDF$sector, 
                             "<br>","Region: ", PlanetsDF$region,
                             "<br>","Coordinates: (", PlanetsDF$x,",", PlanetsDF$y,")")

#changing Region names so they have proper letter case or same name  
PlanetsDF$region<-
  ifelse(PlanetsDF$region=="Expansion Regions","Expansion Region",
  ifelse(PlanetsDF$region=="Inner RIm","Inner Rim",
  ifelse(PlanetsDF$region=="Outer RIm","Outer Rim",
         PlanetsDF$region<-as.character(PlanetsDF$region))))

#changing the binary values to the two cases of Legends or Canon
PlanetsDF$canon<- ifelse(PlanetsDF$canon==0,"Legends","Canon")  

#My attempt to create use list of planets that are significant or commonly know
#to the star wars universe. In order to adjust so these planets are easily visible 
#I am adjusting the zooms by making uncommon legends planets less visible (+1 zoom)
#while making important planets more visible(*0) but keeping the rest the same values
PlanetsDF$alterzm<-ifelse(PlanetsDF$name %in% Alterations,PlanetsDF$zm*0,
                   ifelse(PlanetsDF$canon=="Legends",PlanetsDF$zm+1,
                          PlanetsDF$zm<-PlanetsDF$zm))
#Reordering the values of zoom  
PlanetsDF$alterzm<-ifelse(PlanetsDF$alterzm == 0 , 6, 
                   ifelse(PlanetsDF$alterzm == 1 , 5,
                   ifelse(PlanetsDF$alterzm == 2 , 4,
                   ifelse(PlanetsDF$alterzm == 3 , 3,
                   ifelse(PlanetsDF$alterzm == 4 , 2,
                   ifelse(PlanetsDF$alterzm == 5 , 1, 0))))))

#Using for important planets to show labels
Important<-PlanetsDF %>% filter(alterzm==6)

#changing the value of mid rim so it has proper letter case
levels(Regions@data[["region"]])[levels(Regions@data[["region"]])=="Mid RIm"]<-"Mid Rim"
#Curious to see which Regions have the most planets
PlanetsPerRegion<-PlanetsDF %>% filter(region !="") %>%  
  group_by(region) %>% 
  summarise(TotalPlanets=n()) %>% 
  arrange(desc(TotalPlanets))

knitr::kable(PlanetsPerRegion,"html") %>% 
  kable_styling(bootstrap_options = "striped", full_width = F)
region TotalPlanets
Outer Rim 808
Mid Rim 395
Inner Rim 314
Expansion Region 162
Core 151
Colonies 99
Hutt Space 85
Wild Space 51
Unknown Regions 36
Deep Core 32
{#Color Palettes used for the map's legend and the different regions 
pal <- colorFactor(c("orange", "navy"), domain = c("Legends", "Canon"))#Legend
pal2 <- colorBin("YlOrRd", domain = Regions$rid)
pal3 <- colorBin("YlGnBu", domain = Regions$rid)#This palette looks the best 
pal4 <- colorBin("GnBu", domain = Regions$rid)
}
GalacticMap<-leaflet() %>% 
    
    
    setView(0, -25, zoom = 1.60)%>% 
      
    #Grid
    addPolylines(data = Grid,group = "Grid",
               opacity = .75,color = "black",weight = .75) %>%
    #Regions
    addPolygons(data = Regions,stroke = TRUE,smoothFactor = 0.3,
              weight = 1,color = "white",
              fillOpacity = 0.75,fillColor = ~pal3(rid),
              popup = Regions$region,
              group = "Regions",
              highlight = highlightOptions(
                weight = 5,
                color = "#666",
                dashArray = "",
                fillOpacity = 0.25,
                bringToFront = FALSE)) %>%
    
    #Sectors  
    addPolygons(data = Sectors,stroke = TRUE,smoothFactor = 0.3,
                    weight = 1,color = "grey50",group = "Sectors",
                    fillOpacity = 0,fillColor = "grey",dashArray = "3",
                    popup = Sectors$sector,
                    highlight = highlightOptions(
                      weight = 5,
                      color = "#666",
                      dashArray = "",
                      fillOpacity = 0,
                      bringToFront = FALSE))%>% 
    
    #Hyperspace Lanes
      #In order for the larger hyperspace lanes to stand out from the rest
      #Used zoom levels to change the opacity and weight of the lines
    addPolylines(data = Hyperspace,group = "Hyperspace",
               opacity = 1/Hyperspace$zoom_level,weight = 3/(Hyperspace$zoom_level),
               label = Hyperspace$hyperspace,color = "magenta") %>%
    
    #Planets
      #In order for the more commonly known planets to stand out from the rest
      #Used zoom levels to change the opacity and weight of the circles 
    addCircleMarkers(data = PlanetsDF, group = "Planets",
                   label = PlanetsDF$name, popup = PlanetsDF$content,
                   radius = PlanetsDF$alterzm/2.4,color = ~pal(PlanetsDF$canon),
                   stroke = FALSE, fillOpacity = PlanetsDF$alterzm/6) %>% 
    
    #Labels
      #Labels for planets I deemed to be more commonly known planets
    addLabelOnlyMarkers(data = Important, group = "Planets", label = Important$name,
                        labelOptions = labelOptions(noHide = T, textOnly = TRUE)) %>% 
    
    #Map Legend
      #In order to tell which planets are legends or canon 
    addLegend(position = "bottomright",pal = pal,values = PlanetsDF$canon) %>%
    
    #Layer Control
      #A panel to select which layers to view
    addLayersControl(
      overlayGroups = c("Regions","Sectors", "Planets","Hyperspace","Grid"),
      options = layersControlOptions(collapsed = FALSE)) %>% 
  
    #A measuring feature that can be added 
    #addMeasure() %>% 
    
    #Home Button  
      #Essentially a return to original zoom and origin
    addEasyButtonBar(

      easyButton(
        icon='fa-crosshairs', title='Locate Me',
        onClick=JS("function(btn, map){var center=[0,0]; map.setView(center);
                   map.setZoom(1.6);}")))

GalacticMap